import unittest

from PyQt4 import QtGui

from limber.ui import mainwindow
from limber.viewdisplayers import tabbedviewdisplayer
from mocks import *
from tools import ConnectionBox


app = None

def setup_module():
    global app
    tabbedviewdisplayer._original_displayer = tabbedviewdisplayer.TabbedViewDisplayer
    tabbedviewdisplayer.TabbedViewDisplayer = MockDisplayerGUI
    app = QtGui.QApplication([])

def teardown_module():
    tabbedviewdisplayer.TabbedViewDisplayer = tabbedviewdisplayer._original_displayer
    del tabbedviewdisplayer._original_displayer


class TestMainWindow(unittest.TestCase):
    def setUp(self):
        self.mainwindow = mainwindow.MainWindow(None)
        self.box = ConnectionBox()
    
    def testEmitsNewDocumentRequest(self):
        self.mainwindow.create_new_document_request.connect(self.box.slot)
        
        self.mainwindow.file_menu_actions[0].trigger()
        
        self.assertEqual(self.box.signals_arrived, 1)
    
    def testEmitsCloseCurrentDocumentRequest(self):
        self.mainwindow.close_current_document_request.connect(self.box.slot)
        
        self.mainwindow.file_menu_actions[-3].trigger()
        
        self.assertEqual(self.box.signals_arrived, 1)
    
    def testEmitsApplicationClosureRequest(self):
        self.mainwindow.application_closure_request.connect(self.box.slot)
        
        self.mainwindow.close()
        self.mainwindow.file_menu_actions[-1].trigger()
        
        self.assertEqual(self.box.signals_arrived, 2)
